package data;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

public class OrderElement {
	private int mProductId;
	private double mPrice;
	private int mQuantity;
	private boolean mReinvoice;
	
	public OrderElement(int productId, double price, int quantity, boolean reinvoice) {
		mProductId = productId;
		mPrice = price;
		mQuantity = quantity;
		mReinvoice = reinvoice;
	}
	
	public int getProductId() {
		return mProductId;
	}
	
	public double getPrice() {
		return mPrice;
	}
	
	public int getQuantity() {
		return mQuantity;
	}
	
	public boolean isReinvoice() {
		return mReinvoice;
	}
	
	public final static JsonDeserializer<OrderElement> deserializer = new JsonDeserializer<OrderElement>() {
		
		@Override
		public OrderElement deserialize(JsonElement json, Type arg1, JsonDeserializationContext context)
				throws JsonParseException {
			int productId = json.getAsJsonObject().get("product_id").getAsInt();
			double price = json.getAsJsonObject().get("price").getAsDouble();
			int quantity = json.getAsJsonObject().get("quantity").getAsInt();
			boolean reinvoice = json.getAsJsonObject().get("reinvoice").getAsInt() == 1;
			return new OrderElement(productId, price, quantity, reinvoice);
		}
	};
	
	public final static JsonSerializer<OrderElement> serializer = new JsonSerializer<OrderElement>() {
		
		@Override
		public JsonElement serialize(OrderElement arg0, Type arg1, JsonSerializationContext arg2) {
			JsonObject obj = new JsonObject();
			obj.addProperty("mProductId", arg0.getProductId());
			obj.addProperty("mPrice", arg0.getPrice());
			obj.addProperty("mQuantity", arg0.getQuantity());
			obj.addProperty("mReinvoice", arg0.isReinvoice());
			return obj;
		}
	};
}
